package com.zzq.my.jetpacks.jectpackapp.ui.act

import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.adapter.RvAdapter
import com.zzq.my.jetpacks.jectpackapp.bean.ItemInfo
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityStickylayoutBinding
import com.zzq.my.jetpacks.jectpackapp.view.StickyLayout

/**
 * 描述：同一方向上有双层滚动
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class StickyLayoutActivity : BaseActivity(R.layout.activity_stickylayout) {
    private val vb by viewBinding(ActivityStickylayoutBinding::bind)
    private lateinit var rvAdapter: RvAdapter
    /**
     * 初始化数据
     */
    override fun initData() {
        rvAdapter = RvAdapter()
        val mutableListOf = mutableListOf<ItemInfo>()
        val listOf = listOf(
            "网络请求封装",
            "ConcatAdapter合并多个rv适配器",
            "androidx Fragment 懒加载",
            "FlexboxLayout弹性布局",
            "Rxhttp 网络请求封装",
            "ShapeView阴影基本用法",
            "MMKV 的基本使用",
            "Flow 的基本是使用",
            "fresco 强大的图片加载框架",
            "APP 引导页",
            "fresco 强大的图片加载框架",
            "ActivityMessenger简化Activity之间传参和回调的逻辑代码。",
            "step指示器动态添加",
            "到账方式",
            "账号提现",
            " 实现带空格分隔的输入电话号码",
            " NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端",
            " ViewPager listView 滑动冲突解决",
            "网络请求封装",
            "ConcatAdapter合并多个rv适配器",
            "androidx Fragment 懒加载",
            "FlexboxLayout弹性布局",
            "Rxhttp 网络请求封装",
            "ShapeView阴影基本用法",
            "MMKV 的基本使用",
            "Flow 的基本是使用",
            "fresco 强大的图片加载框架",
            "APP 引导页",
            "fresco 强大的图片加载框架",
            "ActivityMessenger简化Activity之间传参和回调的逻辑代码。",
            "step指示器动态添加",
            "到账方式",
            "账号提现",
            " 实现带空格分隔的输入电话号码",
            " NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端",
            " ViewPager listView 滑动冲突解决",
            "网络请求封装",
            "ConcatAdapter合并多个rv适配器",
            "androidx Fragment 懒加载",
            "FlexboxLayout弹性布局",
            "Rxhttp 网络请求封装",
            "ShapeView阴影基本用法",
        )
        listOf.forEach {
            mutableListOf.add(ItemInfo(it))
        }

//        binding.rvContent1.setHasFixedSize(true);
//        binding.rvContent1.setNestedScrollingEnabled(false);

        rvAdapter.setList(mutableListOf)
        //添加自定义分割线：可自定义分割线高度和颜色
//        vb.stickyContent.addItemDecoration(
////            DividerItemUtils.dividerItemDecorationTool(mContext)//默认的间隔线
//            DividerItemUtils.dividerItemDecorationTool(
//                mContext,
//                drawId = R.drawable.divider_mileage
//            )//自定义间隔线
//        )
        vb.stickyContent.adapter = rvAdapter
        skicky()
    }

    /**
     * 初始化操作
     */
    override fun initView() {

    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {

    }

    /**
     * 任务的列表可向上滑动
     */
    private fun skicky() {
        vb.stickyLayout.setOnGiveUpTouchEventListener(object :
            StickyLayout.OnGiveUpTouchEventListener {
            override fun giveUpTouchEvent(event: MotionEvent?): Boolean {
                val layoutManager: RecyclerView.LayoutManager? = vb.stickyContent.layoutManager
                val linearManager = layoutManager as LinearLayoutManager
                //获取最后一个可见view的位置
                val lastItemPosition = linearManager.findLastVisibleItemPosition()
                //获取第一个可见view的位置
                val firstItemPosition = linearManager.findFirstVisibleItemPosition()
                //getFirstVisiblePosition --->  ListView
                if ( firstItemPosition === 0) {
                    val view: View = vb.stickyContent.getChildAt(0)
                    if (view != null && view.top >= 0) {
                        return true
                    }
                }
                return false
            }
        })
    }
}